package com.apache.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.apache.api.vo.ParamsVo;
import com.apache.api.vo.ResultEntity;
import com.apache.cache.util.Validator;
import com.apache.database.db.IDao;
import com.apache.database.model.MethodParam;
import com.apache.database.model.Page;
import com.apache.exception.BusinessException;
import com.apache.order.entity.TOrder;
import com.apache.order.manager.TOrderManager;
import com.apache.rpc.util.LoadRpcService;

/**
 * description:  订单主信息
 * @author Hou Dayu 创建时间：2015-8-3  
 */
public class TOrderManagerImpl implements TOrderManager {

	protected IDao tOrderDao;
	protected final String entityName = "com.apache.order.entity.TOrder";

	public void settOrderDao(IDao tOrderDao) {
		this.tOrderDao = tOrderDao;
	}

	public String saveInfo(ParamsVo<TOrder> vo) throws BusinessException {
		TOrder tOrderMain = vo.getObj();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("seqEname", "order");
		map.put("strLen", "15");
		map.put("seqMode", "1");
		String infoId = Validator.generate();
		ResultEntity entity = LoadRpcService.service().doService("seqService", "seqNextValue", "xml", map, null);
		if (Validator.isNotNull(String.valueOf(entity.getEntity()))) {
			infoId = String.valueOf(entity.getEntity());
		}
		//定义对象缓存KEY,如果不需要缓存对象请不要对变量赋值，如果要缓存对象建议使用infoId
		String cacheKey = "";
		//String cacheKey="tOrderMain_"+infoId;
		tOrderMain.setOrderId(infoId);
		MethodParam param = new MethodParam("TOrderMain", cacheKey, "", entityName);
		param.setVaule(tOrderMain);
		if (tOrderDao.insert(param)) {
			return infoId;
		}
		return "";
	}

	public boolean editInfo(ParamsVo<TOrder> vo) throws BusinessException {
		TOrder tOrderMain = vo.getObj();
		if (Validator.isNotNull(tOrderMain.getOrderId())) {
			String key = Validator.getDefaultStr(vo.getMethodKey(), "TOrder");
			if ("TOrderStatus".equalsIgnoreCase(key)) {
				MethodParam param = new MethodParam(key, "", "", entityName);
				if (Validator.isNotNull(tOrderMain.getOrderStatus())) {
					param.setParams("orderStatus", tOrderMain.getOrderStatus());
				}
				if (Validator.isNotNull(tOrderMain.getPayStatus())) {
					param.setParams("payStatus", tOrderMain.getPayStatus());
				}
				if (Validator.isNotNull(tOrderMain.getDealPrice())) {
					param.setParams("dealPrice", tOrderMain.getDealPrice());
				}
				return tOrderDao.edit(param);
			} else {
				String cacheKey = "";
				//String cacheKey="tOrderMain_"+tOrderMain.getOrderId();
				MethodParam param = new MethodParam(key, cacheKey, "", entityName);
				param.setVaule(tOrderMain);
				boolean mark = tOrderDao.edit(param);
				/*if (mark && !Validator.isEmpty(vo.getParams("product"))) {
					TOrderDetail product = (TOrderDetail) vo.getParams("product");
					param = new MethodParam("TOrderProduct", cacheKey, "", TOrderDetail.class.getName());
					param.setVaule(product);
					mark = tOrderDao.edit(param);
				}*/
				return mark;
			}
		}
		return false;
	}

	public boolean deleteInfo(ParamsVo<TOrder> vo) throws BusinessException {
		String infoId = vo.getInfoId();
		if (Validator.isNull(infoId)) {
			return false;
		}
		String cacheKey = "";
		//String cacheKey="tOrderMain_"+infoId;
		String mark = Validator.getDefaultStr(String.valueOf(vo.getParams("isDelete")), "true");
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		TOrder info = (TOrder) tOrderDao.selectById(param);
		if (Validator.isEmpty(info)) {
			return false;
		}
		param.setVaule(info);//此处需要先将状态值赋值为删除状态
		if ("false".equals(mark)) {//逻辑删除
			param.setKey("TOrderMain");
			return tOrderDao.edit(param);
		} else {
			param.setParams("orderId", infoId);
			param.setDelete(true);
			return tOrderDao.delete(param);
		}
	}

	public Object getInfoById(ParamsVo<TOrder> vo) {
		String infoId = vo.getInfoId();
		String cacheKey = "";
		//String cacheKey="tOrderMain_"+infoId;
		if (Validator.isNull(infoId)) {
			return null;
		}
		MethodParam param = new MethodParam("ById", cacheKey, "", entityName);
		param.setInfoId(infoId);
		return tOrderDao.selectById(param);
	}

	public Object execute(ParamsVo<TOrder> vo) {
		// TODO Auto-generated method stub
		return null;
	}

	public Page getPageInfo(ParamsVo<TOrder> vo) {
		MethodParam param = setMethodParams(vo, 2);
		int pageSize = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageSize")), "10"));
		int pageIndex = Integer.valueOf(Validator.getDefaultStr(String.valueOf(vo.getParams("pageIndex")), "1"));
		param.setPageIndex(pageIndex);
		param.setPageSize(pageSize);
		String orderby = Validator.getDefaultStr(String.valueOf(vo.getParams("orderby")), "created_time desc");
		param.setParams("orderBy", orderby);
		Page page = tOrderDao.pageSelect(param);
		return page;
	}

	public List<TOrder> getList(ParamsVo<TOrder> vo) {
		MethodParam param = setMethodParams(vo, 2);
		String orderby = Validator.getDefaultStr(String.valueOf(vo.getParams("orderby")), "created_time desc");
		param.setParams("orderBy", orderby);
		return tOrderDao.select(param);
	}

	public long countInfo(ParamsVo<TOrder> vo) {
		MethodParam param = setMethodParams(vo, 1);
		return tOrderDao.count(param);
	}

	private MethodParam setMethodParams(ParamsVo<TOrder> vo, int type) {
		String methodKey = Validator.getDefaultStr(vo.getMethodKey(), "ByProperty");
		StringBuffer sb = new StringBuffer(tOrderDao.getSql(type));
		MethodParam param = new MethodParam(methodKey, "", "", entityName);
		TOrder tOrderMain = vo.getObj();
		if (!Validator.isEmpty(tOrderMain)) {
			if (Validator.isNotNull(tOrderMain.getOrderId())) {
				param.setParams("orderId", tOrderMain.getOrderId());
			}
			if (Validator.isNotNull(tOrderMain.getBuyUserEname())) {
				param.setParams("buyUserEname", tOrderMain.getBuyUserEname());
			}
			if (Validator.isNotNull(tOrderMain.getBuyUserCname())) {
				param.setParams("buyUserCname", tOrderMain.getBuyUserCname());
			}
			if (Validator.isNotNull(tOrderMain.getSellUserEname())) {
				param.setParams("sellUserEname", tOrderMain.getSellUserEname());
			}
			if (Validator.isNotNull(tOrderMain.getSellUserCname())) {
				param.setParams("sellUserCname", tOrderMain.getSellUserCname());
			}
			if (Validator.isNotNull(tOrderMain.getOrderStatus())) {
				param.setParams("orderStatus", tOrderMain.getOrderStatus());
			}
			if (Validator.isNotNull(tOrderMain.getPayStatus())) {
				param.setParams("payStatus", tOrderMain.getPayStatus());
			}
			if (Validator.isNotNull(tOrderMain.getOrderContent())) {
				param.setParams("orderContent", tOrderMain.getOrderContent());
			}
			if (Validator.isNotNull(tOrderMain.getOrderType())) {
				param.setParams("orderType", tOrderMain.getOrderType());
			}
			if (Validator.isNotNull(tOrderMain.getPayMethod())) {
				param.setParams("payMethod", tOrderMain.getPayMethod());
			}
			if (Validator.isNotNull(tOrderMain.getTransferType())) {
				param.setParams("transferType", tOrderMain.getTransferType());
			}
			if (Validator.isNotNull(tOrderMain.getOrderSp1())) {
				param.setParams("orderSp1", tOrderMain.getOrderSp1());
			}
			if (Validator.isNotNull(tOrderMain.getOrderSp2())) {
				param.setParams("orderSp2", tOrderMain.getOrderSp2());
			}
			if (Validator.isNotNull(tOrderMain.getOrderSp3())) {
				param.setParams("orderSp3", tOrderMain.getOrderSp3());
			}
		}
		String orderStatuses = String.valueOf(vo.getParams("orderStatuses"));
		if (Validator.isNotNull(orderStatuses)) {
			param.setParams("orderStatuses", orderStatuses);
		}

		String channel = String.valueOf(vo.getParams("channels"));
		if (Validator.isNotNull(channel)) {
			param.setParams("channels", channel);
		}

		param.setSqlStr(sb.toString());
		return param;
	}
}
